package styledaco.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import comm.util.RequestManager;
import comm.util.SoptHashMap;

import statics.SoptVars;
import styledaco.auction.AuctionProc;
import styledaco.member.MemberProc;
import sun.rmi.server.Dispatcher;

public class TenderServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1959881076273176558L;
	private Logger logger = Logger.getLogger(TenderServlet.class);
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SoptHashMap hm = RequestManager.getRequest(request);
		
		AuctionProc auction = new AuctionProc();
		
		SoptHashMap goodHm = auction.getGoodsBySeq(hm.get("GOODS_SEQ").toString());
		
		
		String tenderResult = "";
		Calendar cal = Calendar.getInstance(Locale.KOREA);
		Date date = cal.getTime();
		Date endDate = null;
		if(goodHm.get("GOODS_AWARD_DATETIME")!=null){
			int endDateArr[] = {
				Integer.parseInt(goodHm.get("GOODS_AWARD_DATETIME").toString().substring(0,4))-1900,
				Integer.parseInt(goodHm.get("GOODS_AWARD_DATETIME").toString().substring(5,7))-1,
				Integer.parseInt(goodHm.get("GOODS_AWARD_DATETIME").toString().substring(8,10)),
				Integer.parseInt(goodHm.get("GOODS_AWARD_DATETIME").toString().substring(11,13)),
				Integer.parseInt(goodHm.get("GOODS_AWARD_DATETIME").toString().substring(14,16)),
				Integer.parseInt(goodHm.get("GOODS_AWARD_DATETIME").toString().substring(17,19))
			};
			endDate = new Date(endDateArr[0],endDateArr[1],endDateArr[2],endDateArr[3],endDateArr[4],endDateArr[5]);
		}
		
		if(!date.before(endDate)){
			tenderResult = "마감된 상품입니다.";
		}else{
			if(hm.get("TENDER_COST")!=null && hm.get("TENDER_GOODS_SEQ")!=null){
				goodHm.put("TENDER_COST", hm.get("TENDER_COST"));
				goodHm.put("TENDER_GOODS_SEQ", hm.get("TENDER_GOODS_SEQ"));
				//입찰수행-
				logger.debug("입찰수행하자!");			
				
				if(request.getSession().getAttribute("SESSION_MEMBER_SEQ")!=null &&
						Integer.parseInt(goodHm.get("TENDER_COST").toString())>0){
					String curMemSeq = request.getSession().getAttribute("SESSION_MEMBER_SEQ").toString();
					goodHm.put("MEMBER_SEQ", curMemSeq);
					MemberProc member = new MemberProc();
					if(member.updateMemberCoin(curMemSeq,(-1)*Integer.parseInt(goodHm.get("TENDER_COST").toString()))>0){
						tenderResult = auction.regTender(curMemSeq, goodHm.get("TENDER_GOODS_SEQ").toString(), hm.get("TENDER_COST").toString());
						if(tenderResult.length()>0)tenderResult = SoptVars.SUCCESS;
					}
					else tenderResult = "땅콩이 모자랍니다!";
				}else{
					tenderResult = "접속중이 아니거나, 땅콩이 0개 입니다.";
				}
			}
		}
		
		goodHm.put("TENDER_RESULT", tenderResult);
		
		RequestDispatcher dispatcher = getServletConfig().getServletContext().getRequestDispatcher("/jsp/commons/tenderForm.jsp");
		dispatcher.forward(RequestManager.mapToServletRequest(request, goodHm), response);
	}

}
